﻿using System;
using System.IO;
using Terraria.Localization;
using Terraria.ModLoader.IO;
using Terraria.Utilities;

namespace Terraria.IO
{
	// Token: 0x02000331 RID: 817
	public class WorldFileData : FileData
	{
		// Token: 0x1700021B RID: 539
		// (get) Token: 0x06001EB9 RID: 7865 RVA: 0x00015C2D File Offset: 0x00013E2D
		public string SeedText
		{
			get
			{
				return this._seedText;
			}
		}

		// Token: 0x1700021C RID: 540
		// (get) Token: 0x06001EBA RID: 7866 RVA: 0x00015C35 File Offset: 0x00013E35
		public int Seed
		{
			get
			{
				return this._seed;
			}
		}

		// Token: 0x1700021D RID: 541
		// (get) Token: 0x06001EBB RID: 7867 RVA: 0x00015C3D File Offset: 0x00013E3D
		public string WorldSizeName
		{
			get
			{
				return this._worldSizeName.Value;
			}
		}

		// Token: 0x1700021E RID: 542
		// (get) Token: 0x06001EBC RID: 7868 RVA: 0x00015C4A File Offset: 0x00013E4A
		// (set) Token: 0x06001EBD RID: 7869 RVA: 0x00015C55 File Offset: 0x00013E55
		public bool HasCrimson
		{
			get
			{
				return !this.HasCorruption;
			}
			set
			{
				this.HasCorruption = !value;
			}
		}

		// Token: 0x1700021F RID: 543
		// (get) Token: 0x06001EBE RID: 7870 RVA: 0x00015C61 File Offset: 0x00013E61
		public bool HasValidSeed
		{
			get
			{
				return this.WorldGeneratorVersion > 0UL;
			}
		}

		// Token: 0x17000220 RID: 544
		// (get) Token: 0x06001EBF RID: 7871 RVA: 0x00015C6D File Offset: 0x00013E6D
		public bool UseGuidAsMapName
		{
			get
			{
				return this.WorldGeneratorVersion >= 777389080577UL;
			}
		}

		// Token: 0x06001EC0 RID: 7872 RVA: 0x00015C83 File Offset: 0x00013E83
		public WorldFileData() : base("World")
		{
		}

		// Token: 0x06001EC1 RID: 7873 RVA: 0x00015CA9 File Offset: 0x00013EA9
		public WorldFileData(string path, bool cloudSave) : base("World", path, cloudSave)
		{
		}

		// Token: 0x06001EC2 RID: 7874 RVA: 0x00015CD1 File Offset: 0x00013ED1
		public override void SetAsActive()
		{
			Main.ActiveWorldFileData = this;
		}

		// Token: 0x06001EC3 RID: 7875 RVA: 0x0044720C File Offset: 0x0044540C
		public void SetWorldSize(int x, int y)
		{
			this.WorldSizeX = x;
			this.WorldSizeY = y;
			if (x == 4200)
			{
				this._worldSizeName = Language.GetText("UI.WorldSizeSmall");
				return;
			}
			if (x == 6400)
			{
				this._worldSizeName = Language.GetText("UI.WorldSizeMedium");
				return;
			}
			if (x != 8400)
			{
				this._worldSizeName = Language.GetText("UI.WorldSizeUnknown");
				return;
			}
			this._worldSizeName = Language.GetText("UI.WorldSizeLarge");
		}

		// Token: 0x06001EC4 RID: 7876 RVA: 0x00447284 File Offset: 0x00445484
		public static WorldFileData FromInvalidWorld(string path, bool cloudSave)
		{
			WorldFileData worldFileData = new WorldFileData(path, cloudSave);
			worldFileData.IsExpertMode = false;
			worldFileData.SetSeedToEmpty();
			worldFileData.WorldGeneratorVersion = 0UL;
			worldFileData.Metadata = FileMetadata.FromCurrentSettings(FileType.World);
			worldFileData.SetWorldSize(1, 1);
			worldFileData.HasCorruption = true;
			worldFileData.IsHardMode = false;
			worldFileData.IsValid = false;
			worldFileData.Name = FileUtilities.GetFileName(path, false);
			worldFileData.UniqueId = Guid.Empty;
			if (!cloudSave)
			{
				worldFileData.CreationTime = File.GetCreationTime(path);
			}
			else
			{
				worldFileData.CreationTime = DateTime.Now;
			}
			return worldFileData;
		}

		// Token: 0x06001EC5 RID: 7877 RVA: 0x00015CD9 File Offset: 0x00013ED9
		public void SetSeedToEmpty()
		{
			this.SetSeed("");
		}

		// Token: 0x06001EC6 RID: 7878 RVA: 0x00015CE6 File Offset: 0x00013EE6
		public void SetSeed(string seedText)
		{
			this._seedText = seedText;
			if (!int.TryParse(seedText, out this._seed))
			{
				this._seed = seedText.GetHashCode();
			}
			this._seed = Math.Abs(this._seed);
		}

		// Token: 0x06001EC7 RID: 7879 RVA: 0x0044730C File Offset: 0x0044550C
		public void SetSeedToRandom()
		{
			this.SetSeed(new UnifiedRandom().Next().ToString());
		}

		// Token: 0x06001EC8 RID: 7880 RVA: 0x00447334 File Offset: 0x00445534
		public override void MoveToCloud()
		{
			if (base.IsCloudSave)
			{
				return;
			}
			string worldPathFromName = Main.GetWorldPathFromName(this.Name, true);
			if (FileUtilities.MoveToCloud(base.Path, worldPathFromName))
			{
				WorldIO.MoveToCloud(base.Path, worldPathFromName);
				Main.LocalFavoriteData.ClearEntry(this);
				this._isCloudSave = true;
				this._path = worldPathFromName;
				Main.CloudFavoritesData.SaveFavorite(this);
			}
		}

		// Token: 0x06001EC9 RID: 7881 RVA: 0x00447398 File Offset: 0x00445598
		public override void MoveToLocal()
		{
			if (!base.IsCloudSave)
			{
				return;
			}
			string worldPathFromName = Main.GetWorldPathFromName(this.Name, false);
			if (FileUtilities.MoveToLocal(base.Path, worldPathFromName))
			{
				WorldIO.MoveToLocal(base.Path, worldPathFromName);
				Main.CloudFavoritesData.ClearEntry(this);
				this._isCloudSave = false;
				this._path = worldPathFromName;
				Main.LocalFavoriteData.SaveFavorite(this);
			}
		}

		// Token: 0x0400199F RID: 6559
		private const ulong GUID_IN_WORLD_FILE_VERSION = 777389080577UL;

		// Token: 0x040019A0 RID: 6560
		public DateTime CreationTime;

		// Token: 0x040019A1 RID: 6561
		public int WorldSizeX;

		// Token: 0x040019A2 RID: 6562
		public int WorldSizeY;

		// Token: 0x040019A3 RID: 6563
		public ulong WorldGeneratorVersion;

		// Token: 0x040019A4 RID: 6564
		private string _seedText = "";

		// Token: 0x040019A5 RID: 6565
		private int _seed;

		// Token: 0x040019A6 RID: 6566
		public bool IsValid = true;

		// Token: 0x040019A7 RID: 6567
		public Guid UniqueId;

		// Token: 0x040019A8 RID: 6568
		public LocalizedText _worldSizeName;

		// Token: 0x040019A9 RID: 6569
		public bool IsExpertMode;

		// Token: 0x040019AA RID: 6570
		public bool HasCorruption = true;

		// Token: 0x040019AB RID: 6571
		public bool IsHardMode;
	}
}
